package cn.itsource.user.service.impl;

import cn.itsource.basic.service.impl.BaseServiceImpl;
import cn.itsource.user.domain.User;
import cn.itsource.user.domain.vo.UserVo;
import cn.itsource.user.mapper.UserMapper;
import cn.itsource.user.service.IUserService;
import cn.itsource.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.UUID;

@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public User loadByPhone(String phone) {
        return userMapper.loadByPhone(phone);
    }

    /*
         code	"123546"
        password	"123"
        passwordRepeat	"123"
        phone	"15008495702"

        1.数据验证
            1.1 验证码不能为空
            1.2 验证 两次密码是否一致
            1.3 验证码是否正确

        2.UserVo- 转化为-User对象
          添加数据
            2.1 用户的状态
            2.2 注册事件
            2.3 密码加密 + 盐



     */
    @Override
    public void registerUser(UserVo userVo) {
        //数据的正确性验证
        verification(userVo);
        //UserVo- 转化为-User对象
        //          添加数据
        //            2.1 用户的状态
        //            2.2 注册事件
        //            2.3 密码加密 + 盐
        User user =userVoToUser(userVo);

        //保存用户对象
        userMapper.save(user);

    }
    //登录的方法
    @Override
    public User login(User user) {
        //1.去数据库里面查找对象
        // select * from t_user WHERE username=#{username} or phone=#{username} or email=#{username}
        User loginUser = userMapper.login(user);
        if (loginUser==null){
            throw  new RuntimeException("用户不存在");
        }else {
            //2.匹配密码是否相等
           // loginUser.getPassword();//加密以后的密码
            //对用户输入的密码进行加密
            String byMd5 = MD5Utils.encrypByMd5(user.getPassword()+loginUser.getSalt());
            if (!byMd5.equals( loginUser.getPassword())){
                throw  new RuntimeException("密码错误");
            }
        }
        return loginUser;


    }

    private User userVoToUser(UserVo userVo) {
        User user = new User();
        //对常规数据的设置
        user.setPhone(userVo.getPhone());
        user.setState(1);
        user.setCreatetime(new Date());
        //获取一个随机字符串 把里面的 “-” 替换为“”
        String uuid = UUID.randomUUID().toString().replace("-", "");
        //密码要进行加密处理  MD5进行加密存储
        String md5Str = MD5Utils.encrypByMd5(userVo.getPassword() + uuid);
        user.setPassword(md5Str);
        user.setSalt(uuid);
        return  user;
    }

    //数据的验证
    private void  verification (UserVo userVo){
        if (userVo.getCode()==null){
            throw  new RuntimeException("验证码不能为空");
        }
        if (!userVo.getPassword().equals(userVo.getPasswordRepeat())){
            throw new RuntimeException("两次密码不一致");
        }
        //验证码是否和redis里面存储的一致
        String redisInCode = (String) redisTemplate.opsForValue().get("REGISTER:"+userVo.getPhone());
        if (redisInCode==null){
            throw new RuntimeException("验证码已经过期");
        }else {
            String code= redisInCode.split(":")[0];
            if (!userVo.getCode().equals(code)){
                throw  new RuntimeException("验证码输入不正确");
            }
        }
    }
}
